<template>
  <h2>种类列表</h2>{{ id }}

  <div class="categorys">
    <el-table :data="categorys" style="width: 100%">
      <el-table-column prop="icon" label="分类图标" width="100"><el-icon><Bowl /></el-icon></el-table-column>
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column prop="name" label="名称" width="120" />
      <!-- <el-table-column prop="showStatus" label="显示状态" width="120" /> -->
      <el-table-column prop="showStatus" label="显示状态" width="120" :filter-method="filterShowStatus"
        filter-placement="bottom-end">
        <template #default="scope">
          <el-tag :type="scope.row.Showstatus === 'Home' ? 'danger' : 'success'" disable-transitions>{{
            scope.row.showStatus == 1 ? '已存在' : '不存在' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="种类" width="120" />
      <el-table-column prop="subjectCount" label="专题数量" />
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="scope">
          <!-- <el-button link type="primary" size="small" @click="toEdit(scope.row)">更新</el-button>
          <el-button link type="primary" size="small" @click="del(scope.row.id)">删除</el-button> -->
          <el-button type="primary" size="small" round @click="toEdit(scope.row)">更新</el-button>
          <el-button type="warning" size="small" round @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <el-pagination layout="prev, pager, next" :page-size="page.size" :total="page.total"
      @current-change="currentchange" /> -->

    <el-pagination
      v-model:current-page="currentPage3"
      v-model:page-size="page.size"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="prev, pager, next, jumper"
      :total="100"
      @size-change="handleSizeChange"
      @current-change="currentchange"
    />
    <div class="category-tools">
      <el-button type="warning" @click="toAdd">添加</el-button>
    </div>
  </div>
  <el-dialog v-model="dialogFormVisible" title="种类编辑">
    <el-form :model="category">
      <el-form-item label="分类图标" :label-width="formLabelWidth">
        <el-input v-model="category.icon" autocomplete="off" />
      </el-form-item>
      <el-form-item label="种类编号" :label-width="formLabelWidth">
        <el-input v-model="category.id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="名称" :label-width="formLabelWidth">
        <el-input v-model="category.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="显示状态" :label-width="formLabelWidth">
        <!-- <el-input v-model="category.showStatus" autocomplete="off" /> -->
        <el-select v-model="category.showStatus" class="m-2" placeholder="Select">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="种类" :label-width="formLabelWidth">
        <el-input v-model="category.sort" autocomplete="off" />
      </el-form-item>
      <el-form-item label="专题数量" :label-width="formLabelWidth">
        <el-input v-model="category.subjectCount" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="save">
          保存
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { defineComponent, ref } from "vue"
import { categoryPage, categoryDelId, categoryAdd, categoryEdit,categoryseek } from "../../http/category";
import { ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'

export default defineComponent({

  data() {
    return {
      categorys: [],
      page: {
        total: 0,
        current: 1,
        size: 200
      },
      dialogFormVisible: false,
      category: {
        'id': 0,
        'name': '',
        'icon': '',
        'subjectCount': '',
        'showStatus': '',
        'sort': ''
      },
      formLabelWidth: 80,
       options: [
        {
          value: '0',
          label: 'Option1',
        },
        {
          value: '1',
          label: 'Option2',
        }]
    }
  },
  mounted() {
    this.getCateGorysPage(1)
  },
  methods: {
    toEdit(category) {
      console.log(category);
      this.dialogFormVisible = true;
      this.category = cloneDeep(category);
    },
    getCateGorysPage(current) {
      const data = {
        current: current,
        size: 5
      }
      categoryPage(data).then(res => {
        console.log(res);
        const page = res.data.page;
        this.categorys = page.records;
        this.page = page;
      }).catch(err => {
        console.log(err);
      })

    },
    currentchange(current) {
      // console.log(current);
      this.getCateGorysPage(current);
      this.page.current = current;//数据更新目前显示的页面
    },
    del(id) {
      //删除数据
      //模拟删除，服务器进行处理 
      //要别人反悔机会
      // 如果只有一条数据，删除数据之后如何处理？
      //人机交互
      console.log(id)
      categoryDelId(id).then(res => {
        if (res.success) {
          this.getCateGorysPage(this.page.current)

        }
        else {
          console.log(res.msg)
          return false
        }


      }).catch(err => {

      })
    },
    toAdd() {
      //到添加的页面
      this.dialogFormVisible = true;
      this.category = {
        'id':'',
        'name': '',
        'icon': '',
        'subjectCount': '',
        'showStatus': '',
        'sort': ''
      }
    },
    save() {
      const cateGory = this.category;
      console.log('---' + JSON.stringify(cateGory))
      if (cateGory.id == 0) {
        categoryAdd(cateGory).then(res => {
          if (res.success) {
            //刷新页面
            this.dialogFormVisible = false;
            this.getCateGorysPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            ElMessage(res.msg)
            return false
          }
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }
      else {
        categoryEdit(cateGory).then(res => {
          if (res.success) {
            //刷新页面
            this.dialogFormVisible = false;
            this.getCateGorysPage(this.page.current)
            ElMessage(res.msg)
          }
          else {
            ElMessage(res.msg)
            return false
          }
        }).catch(err => {
          ElMessage('网络错误联系管理员')
        })
      }

    },

  }
})
</script>

<style lang="scss" scoped></style>
